#region Using Directives

using System;
using System.IO;
using System.Text;

using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

using EnvDTE;
using EnvDTE80;

#endregion

namespace EEst.SoftwareFactory.Actions
{
    class CreateVCPProjectByT4Action : Action
    {
        #region Input Properties

        [Input]
        public string FileContent
        {
            get { return fileContent; }
            set { fileContent = value; }
        } string fileContent;

        [Input]
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        string projectName;
        [Input(Required = false)]
        public string SubFolder
        {
            get { return subFolder; }
            set { subFolder = value; }
        } string subFolder;

        #endregion

        #region Output Properties

        [Output]
        public string ProjectFileName
        {
            get { return projectFileName; }
            set { projectFileName = value; }
        } string projectFileName;

        [Output]
        public Project TargetFolder
        {
            get { return targetFolder; }
            set { targetFolder = value; }
        } Project targetFolder;

        #endregion

        #region IAction Members

        public override void Execute()
        {
            DTE vs = GetService<DTE>(true);
            targetFolder = DteHelper.GetTarget(vs) as Project;
            if (SubFolder != null && SubFolder.CompareTo("") != 0)
            {
                SolutionFolder sFolder = targetFolder.Object as SolutionFolder;
                targetFolder = sFolder.AddSolutionFolder(subFolder);
            }


            Solution2 currentSolution = vs.Solution as Solution2;
            string path = FindTargetProject(currentSolution, targetFolder);
            string solutionPath = (string)currentSolution.Properties.Item("Path").Value;
            string solutionDir = Path.GetDirectoryName(solutionPath);
       //     string targetPath = solutionDir + "\\" + targetFolder.Name;

            string targetPath = solutionDir + "\\" + path;
            string projFolderPath = targetPath + "\\" + projectName;
            string projFileName = projFolderPath + "\\" + projectName + ".vcproj";

            if (!Directory.Exists(projFolderPath))
            {
                Directory.CreateDirectory(projFolderPath);
            }
            if (!File.Exists(projFileName))
            {
                using (StreamWriter writer = new StreamWriter(File.Create(projFileName)))
                {
                    writer.Write(fileContent);
                    writer.Flush();
                }
            }
            projectFileName = projFileName;
        }

        public override void Undo()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        private string FindTargetProject(Solution2 current, Project target)
        {
            string result = null;
            foreach (Project project in current.Projects)
            {
                if (project.Equals(target))
                {
                    return project.Name;
                }
                result = FindSubProject(project, target,project.Name);
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        private string FindSubProject(Project current, Project target, string path)
        {
            if (current.Equals(target))
            {
                return path+"\\"+current.Name;
            }
            string result = null;
            if (current.Object is SolutionFolder)
            {
                foreach (ProjectItem pi in current.ProjectItems)
                {
                    if (pi.Object is Project)
                    {
                        result = FindSubProject((Project)pi.Object,target,path);
                        if (result != null)
                        {
                            break;
                        }
                    }
                }
            }
            return result;
        }
    }
}
